function variableSearchDone(data){
    if(parseInt(data.hits) > 0){
        $("#variable-hits-count").html('('+data.hits+')');
        
        var output = '';
        if(data.variable instanceof Array){
            $.each(data.variable, function(i,variable){
                //console.log('loop ' + i);
                output +=  renderVariable(variable, true);
            });
        }else{
            //console.log('single hit');
            output +=  renderVariable(data.variable, true);
        } 
        
        $("#variable-result").html(output);

        $("#variable-result .referring").click(function(event){
            var link = $(this);
            var id   = $(link).attr('data-id');
            var type = $(link).attr('data-type');
             
            link.addClass('loading');
            
            var data = {
                q:id, 
                type:type,
                format:'json'
            };
            
            $.ajax({
                url: settings.backend + '/ws/referring.xql',
                dataType: 'jsonp',
                data: data,
                success: function(result){
                    var studies = '';
                    if(result.enteties.study instanceof Array)
                        studies = renderStudyTable(result.enteties.study);
                    else
                        studies = renderStudyTable([result.enteties.study]);
                    $(link).prev().html(studies);
                    $(link).prev().find('ul li').sortElements(function(a, b){
                        return $(a).text() > $(b).text() ? 1 : -1;
                    });
                    
                    setActiveLang(settings.active_lang);
                    
                    link.removeClass('loading'); 
                    link.hide();
                }
            });
            
            event.preventDefault();
        });


        setActiveLang(settings.active_lang);
    }else{
        $("#variable-hits-count").html('');
        $("#variable-result").html('');
    }

    $('.variable-tab a').removeClass('loading');      
}

function renderVariable(variable){
    
    var variable_id = '';
    
    if(variable.userid){
        variable_id = variable.userid.id['#text'];
        $.each(variable.userid.id, function(i,id){
             if(id.type == "variable_id")
                 variable_id = id['#text'];
        });
    }    
    
    var output = '<div class="variable-block"><ul>';
     
    output += $.format('<li class="variable-icon"><span class="variable-name">%s</span><p class="lang-strings">', [variable.name.en]);

    $.each(variable.label, function(i,label){
        output += $.format('<span class="%s">%s</span>', [i,label]);
    });
    output += '</p></li>';
    
    output += '</ul><div class="studies"><ul><li class="study"><p class="lang-strings">';
    $.each(variable.study.title, function(i,title){
        output += $.format('<span class="%s"><a href="http://snd.gu.se/%s/catalogue/study/%s">%s</a></span>', [i, i, variable.study.id, title]);
    });
    output += '</p></li></ul></div>';

    output += $.format('<a data-id="%s" data-type="variable_id" href="#" class="variable-info referring" data-localize="get_studies">%s</a></div>', [variable_id, getLocaleString('get_studies')]);

    output += '</ul></div>';
    return output;
}